package com.redribbon.droidnoise;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.content.ContentUris;
import android.content.Intent;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.provider.BaseColumns;
import android.provider.MediaStore;
import android.provider.MediaStore.Audio.ArtistColumns;
import android.provider.MediaStore.Audio.AudioColumns;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class SongPicker extends ListActivity {
	
	class ArtistViewBinder implements SimpleCursorAdapter.ViewBinder {

		public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
			if (view.getId() == R.id.num_albums)
			{
				int num_albums = cursor.getInt(columnIndex);
				String text = num_albums + " album";
				if (num_albums != 1) text += "s";
				((TextView)view).setText(text);
			}
			else if (view.getId() == R.id.num_tracks)
			{
				int num_albums = cursor.getInt(columnIndex);
				String text = num_albums + " track";
				if (num_albums != 1) text += "s";
				((TextView)view).setText(text);
			}
			else
			{
				((TextView)view).setText(cursor.getString(columnIndex));
			}
			return true;
		}
		
	}
	
	private static final String MEDIA_PATH = new String("/sdcard/");
	private List<String> songs = new ArrayList<String>();
	private MediaPlayer mp = new MediaPlayer();
	private int currentPosition = 0;
	private SimpleCursorAdapter adapter = null;
	Cursor cursor;
	String[] columns;

	public final static String[] album_columns = new String[] { 
		AudioColumns.ALBUM, AudioColumns.ARTIST, BaseColumns._ID };
	public final static String[] artist_columns = new String[] { 
		AudioColumns.ARTIST, ArtistColumns.NUMBER_OF_ALBUMS,
		ArtistColumns.NUMBER_OF_TRACKS, BaseColumns._ID,
		AudioColumns.ARTIST_KEY};
	public final static String[] track_columns = new String[] { 
		AudioColumns.TITLE, AudioColumns.ARTIST, AudioColumns.ALBUM, BaseColumns._ID};
	
	public final static int[] artist_ids = new int[] { R.id.artist, R.id.num_albums, R.id.num_tracks };
	public final static int[] album_ids = new int[] { R.id.album, R.id.artist };
	public final static int[] track_ids = new int[] { R.id.track, R.id.artist };
	
	public final static int album_layout = R.layout.album_item;
	public final static int artist_layout = R.layout.artist_item;
	public final static int track_layout = R.layout.song_item;
	    
	class Mp3Filter implements FilenameFilter {
		public boolean accept(File dir, String name) {
			return (name.endsWith(".mp3"));
	    }
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        set_view(MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI, artist_columns, artist_layout, artist_ids, null);
    }
	
     public void onListItemClick(ListView i, View v, int position, long id) {
    	 
    	 Cursor temp_cursor = (Cursor)adapter.getItem(position);
    	 
    	 if (columns.equals(SongPicker.artist_columns)) { // transition from artist to albums
    		 String artist = temp_cursor.getString(temp_cursor.getColumnIndex(AudioColumns.ARTIST));
    		 set_view(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, album_columns, album_layout, album_ids,
    				 AudioColumns.ARTIST + " = '" + artist + "'");
    	 } else if (columns.equals(SongPicker.album_columns)) { // transition from albums to songs
    		 String album = temp_cursor.getString(temp_cursor.getColumnIndex(AudioColumns.ALBUM));
    		 set_view(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, track_columns, track_layout, track_ids,
    				 AudioColumns.ALBUM + " = '" + album + "'");
    	 } else { // clicks a song
    	 
    	 Intent intent = new Intent();
    	 long track_id = temp_cursor.getLong(temp_cursor.getColumnIndex(AudioColumns._ID));
    	 intent.setData(ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, track_id));
    	 setResult(RESULT_OK, intent);
    	 finish();
    	 }
     }

     public boolean onKeyDown(int key_code, KeyEvent event) {
    	 
    	 if (key_code == android.view.KeyEvent.KEYCODE_BACK) { // back button
    		 if (columns.equals(SongPicker.track_columns)) { //transition from tracks to albums
    			 String artist = cursor.getString(cursor.getColumnIndex(AudioColumns.ARTIST));
    			 set_view(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, album_columns, album_layout, album_ids,
    				 AudioColumns.ARTIST + " = '" + artist + "'");
    		 } else if (columns.equals(SongPicker.album_columns)) { //transition from albums to artists
    			 set_view(MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI, artist_columns, artist_layout, artist_ids, null);
    		 }
    		 else
    		 {
    			 setResult(RESULT_CANCELED, new Intent());
    			 finish();
    			 return true;
    		 }
    		 return false;
    	 }
    	 else
    	 {
			 setResult(RESULT_CANCELED, new Intent());
			 finish();
    		 return true;
    	 }
     }
     
     public void set_view(Uri external_uri, String[] new_columns, int layout, int[] new_int_array, String selection) {
    	 
    	  columns = new_columns;
    	  cursor = getContentResolver().query(external_uri, columns,
    			  								selection, null, null);
    	  startManagingCursor(cursor); 
    	  int[] int_array = new_int_array;
    	  adapter = new SimpleCursorAdapter(this, 
    			  layout, 
    			  cursor, 
    			  columns, 
    			  int_array);
    	  
    	  if (columns.equals(artist_columns)) {
    		  adapter.setViewBinder(new ArtistViewBinder());
    	  }
    	  /*
    	  if (columns.equals(artist_columns)) {
    		  adapter.getViewBinder().setViewValue(findViewById(R.id.num_albums), cursor, cursor.getColumnIndex(ArtistColumns.NUMBER_OF_ALBUMS));
    		  adapter.getViewBinder().setViewValue(findViewById(R.id.num_tracks), cursor, cursor.getColumnIndex(ArtistColumns.NUMBER_OF_TRACKS));
    	  }*/
    	  
    	  setListAdapter(adapter);
     }
}

